package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Comment;
import com.itheima.domain.CommentExample;
import com.itheima.mapper.CommentMapper;
import com.itheima.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public PageInfo<Comment> findByPage(Integer pageNum, Integer pageSize, String keyword) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //判断是否有关键字搜索
        if (keyword==null){
            CommentExample commentExample = new CommentExample();
            List<Comment> list = commentMapper.selectByExample(commentExample);
            return new PageInfo<Comment>(list);
        }
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andContentLike("%"+keyword+"%");
        List<Comment> list = commentMapper.selectByExample(commentExample);
        return new PageInfo<Comment>(list);
    }

    @Override
    public PageInfo<Comment> findByPage(Integer pageNum, Integer pageSize, Integer movieId) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andMoviesIdEqualTo(movieId);
        List<Comment> list = commentMapper.selectByExample(commentExample);
        return new PageInfo<Comment>(list);
    }

    @Override
    public List<Comment> findById(Integer id) {
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andMoviesIdEqualTo(id);
        return commentMapper.selectByExample(commentExample);
    }

    @Override
    public void save(Comment comment) {
        comment.setCreatorName("匿名");
        comment.setTime(new Date());
        commentMapper.insertSelective(comment);
    }

    @Override
    public void deleteById(Integer id) {
        commentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteIds(Integer[] ids) {
        commentMapper.deleteIds(ids);
    }
}
